package com.ls.lserp.modules.job.service;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.job.dao.MaterialAlertDao;
import com.ls.lserp.modules.job.entity.MaterialAlertEntity;
import com.ls.lserp.modules.setting.dao.InventoryWarningDao;
import com.ls.lserp.modules.setting.entity.InventoryWarning;
import com.ls.lserp.modules.sys.entity.SendEmailMessage;
import com.ls.lserp.modules.sys.entity.User;
import com.ls.lserp.modules.sys.utils.UserUtils;

/**
 * @ClassName: AccountingService
 * @Description: 原辅料预警数量业务处理
 * 
 * @author 于辉
 * @date 2017年03月12日
 * 
 */
@Service
@Transactional(readOnly = false)
public class MaterialAlertService extends CrudService<MaterialAlertDao, MaterialAlertEntity> {
	@Autowired
	private MaterialAlertDao materialAlertDao;
	@Autowired
	private InventoryWarningDao inventoryWarningDao;

	/**
	 * @Title: 获取当天库存预警数据
	 */
	public List<InventoryWarning> findAllList(InventoryWarning inventoryWarning) throws SQLException {
		return inventoryWarningDao.findAllList(inventoryWarning);
	}
	
	/**
	 * @throws Exception 
	 * @Title: 需要采购的原辅料的明细数据插入
	 */
	public void insertMaterialAlertReserve(InventoryWarning inventoryWarning) throws Exception {
		MaterialAlertEntity mEntity = new MaterialAlertEntity();
		//获取当天库存预警数据
		List<InventoryWarning> materialAlertList = findAllList(inventoryWarning);
		if (materialAlertList != null && materialAlertList.size() > 0) {
			String materialNameStr = "";
			mEntity.setCreateBy(new User());
			for (int i = 0; i < materialAlertList.size(); i++) {
				//库存数量与库存预警数量进行比较，如果结果是-1 则 小于,0 则 等于,1 则 大于。
				int compareResult = materialAlertList.get(i).getMaterialQuantity().compareTo(new BigDecimal(materialAlertList.get(i).getMaterialAlertQuantity()));
				//如果库存数量小于等于库存预警数量，则产生库存预警数据
				if (compareResult == 0 || compareResult == -1) {
					mEntity.setAlertId(materialAlertList.get(i).getId());
					mEntity.setMaterialId(materialAlertList.get(i).getMaterialId());
					mEntity.setMaterialLeastQuantity(materialAlertList.get(i).getMaterialLeastQuantity());
					mEntity.setMaterialStatus(ResourcesUtil.getConfig("inventoryAlert_00"));
					mEntity.setCreateDate(new Date());
					mEntity.getCreateBy().setId(1L);
					materialAlertDao.insertMaterialAlertReserve(mEntity);
					if (!"".equals(materialNameStr)) {
						materialNameStr = "," + materialNameStr;
					}
					materialNameStr = materialAlertList.get(i).getMaterialCode() + materialNameStr;
				}
			}
			if (!"".equals(materialNameStr)) {
				String url =  ResourcesUtil.getConfig("address")+"/stock/warning";
				SendEmailMessage sendEmailMessage = new SendEmailMessage();
				sendEmailMessage.setDataTheme(ResourcesUtil.getConfig("erp_send_mail_theme08"));
				sendEmailMessage.setDataMessage(ResourcesUtil.getConfig("erp_send_mail_textMessage08"));
				sendEmailMessage.setDataYYMMDD01(DateUtils.getDate("yyyy-MM-dd"));
				sendEmailMessage.setDataXXX02(materialNameStr);
				sendEmailMessage.setDataXURLX(url);
				sendEmailMessage.setMailFlag("materialAlert");
				//发送邮件
				UserUtils.sendEmail(sendEmailMessage);
			}
		}
		
	}
}
